package mysql

import (
	"errors"
	"fmt"
	"my_chat/model"
	"time"
)

func AddFriends(o int64, d int64) (data interface{}, err error) {
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	// 添加好友记录
	c1 := model.Contact{
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		Ownerid:   o,
		Dstobj:    d,
		Cate:      model.CONCAT_CATE_USER,
	}
	if err := tx.Create(&c1).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	c2 := model.Contact{
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		Ownerid:   d,
		Dstobj:    o,
		Cate:      model.CONCAT_CATE_USER,
	}
	if err := tx.Create(&c2).Error; err != nil {
		tx.Rollback()
		return nil, err
	}
	if err := tx.Commit().Error; err != nil {
		return nil, err
	}
	return nil, nil
}

// 获取用户好友信息
func GetContact(id int64) (data []model.User, err error) {
	users := new([]model.User)
	contacts := make([]model.Contact, 0)
	// 查询该用户所有contact ids
	contactIds := make([]int64, 0)
	db.Table("contacts").Select("dstobj").Where("ownerid = ? AND cate = ?", id, 1).Find(&contacts)
	for _, v := range contacts {
		contactIds = append(contactIds, v.Dstobj)
	}
	fmt.Printf("%T, %v", contactIds, contactIds)
	err = db.Table("users").Where(contactIds).Find(users).Error
	if err != nil {
		return nil, err
	}
	return *users, nil
}

// 获取用户群聊信息
func GetGroup(id int64) (data []model.Community, err error) {
	communities := new([]model.Community)
	contacts := make([]model.Contact, 0)
	// 查询该用户所有contact ids
	contactIds := make([]int64, 0)
	db.Table("contacts").Select("dstobj").Where("ownerid = ? AND cate = ?", id, 2).Find(&contacts)
	for _, v := range contacts {
		contactIds = append(contactIds, v.Dstobj)
	}
	fmt.Printf("%T, %v", contactIds, contactIds)
	err = db.Table("communities").Where(contactIds).Find(communities).Error
	if err != nil {
		return nil, err
	}
	return *communities, nil
}

// community 通过名称获取 id
func FindCommunityByName(name string) int64 {
	community := new(model.Community)
	db.Select("id").Where("name = ?", name).First(community)
	fmt.Println("community id", community.ID)
	return int64(community.ID)
}

// 判断community是否存在
func IsCommunityExits(name string) bool {
	var count int
	db.Model(&model.Community{}).Where("name = ?", name).Count(&count)
	if count > 0 {
		return true
	}
	return false
}

// 添加community
func CreateCommunity(p *model.Community) (data interface{}, err error) {
	if IsCommunityExits(p.Name) {
		return nil, errors.New("存在相同名字的群，请更换名称！")
	}
	if err := db.Create(p).Error; err != nil {
		return nil, errors.New("添加数据出错")
	}
	fmt.Println("community id", p.ID)
	return int64(p.ID), nil
}

// 查询记录是否存在
func IsExist(o int64, d int64, cate int) (b bool, err error) {
	var count int
	if err := db.Model(&model.Contact{}).Where("dstobj = ? AND ownerid = ? AND cate = ?", d, o, cate).Count(&count).Error; err != nil {
		return false, err
	}
	if count > 0 {
		return true, nil
	}
	return false, nil
}

// 加入群聊
func AddCommunity(o int64, d int64) (data interface{}, err error) {
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	// 添加群聊记录
	c1 := model.Contact{
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		Ownerid:   o,
		Dstobj:    d,
		Cate:      model.CONCAT_CATE_COMUNITY,
	}
	if err := tx.Create(&c1).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	c2 := model.Contact{
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		Ownerid:   d,
		Dstobj:    o,
		Cate:      model.CONCAT_CATE_COMUNITY,
	}
	if err := tx.Create(&c2).Error; err != nil {
		tx.Rollback()
		return nil, err
	}
	if err := tx.Commit().Error; err != nil {
		return nil, err
	}
	return nil, nil
}

// 通过group获取group内用户
func GroupUsers(id int64) (data []int64, err error) {
	contacts := make([]model.Contact, 0)
	contactIds := make([]int64, 0)
	err = db.Table("contacts").Select("dstobj").Where("ownerid = ? AND cate = ?", id, 2).Find(&contacts).Error
	if err != nil {
		return nil, err
	}
	for _, v := range contacts {
		contactIds = append(contactIds, v.Dstobj)
	}
	return contactIds, nil
}
